<!--
 * @Author: zhoulei0232
 * @Date: 2023-04-01 23:03:20
 * @LastEditTime: 2023-04-14 01:21:01
 * @LastEditors: zhoulei0232
 * @Description: 
-->
<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Plus"
          @click="handleAdd"
   
          >新 增</el-button
        >
      </el-col>

      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="pageList"
      @sort-change="handleSortChange"
    >
      <el-table-column label="序号" align="center" type="index" width="100" />
      <el-table-column
        label="字典名称"
        align="center"
        prop="dictName"
        width="200"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="字典编码"
        align="center"
        prop="dictType"
        width="200"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <router-link :to="`/business/dicts/${scope.row.id}`">
            <a href="#" class="blue-color">{{ scope.row.dictType }}</a>
          </router-link>
        </template>
      </el-table-column>

      <el-table-column
        label="启用状态"
        align="center"
        prop="status"
        width="200"
      >
        <template #default="scope">
          <dict-tag :options="approveStatusList" :value="scope.row.status" />
        </template>
      </el-table-column>

      <el-table-column
        label="创建日期"
        align="center"
        prop="createTime"
        width="200"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="修改日期"
        align="center"
        prop="updateTime"
        width="200"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.updateTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        align="center"
        prop="remarks"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="280"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
  
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" v-model="open" width="780px" append-to-body>
      <el-form ref="noticeRef" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="字典名称" prop="dictName">
              <el-input placeholder="请输入字典名称" v-model="form.dictName">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="字典编码" prop="dictType">
              <el-input placeholder="请输入字典编码" v-model="form.dictType">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="form.id">
            <el-form-item label="启用状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in approveList"
                  :key="dict.value"
                  :label="dict.value"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remarks">
              <el-input
                type="textarea"
                :rows="2"
                placeholder="请输入备注"
                v-model="form.remarks"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="debouncedFn">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="SysDicts">
import {
  listSysDicts,
  addSysDicts,
  updateSysDicts,
} from "@/api/business/dicts";
import { useDebounceFn } from "@vueuse/core";

const { proxy } = getCurrentInstance();
// 图片上传地址

const imageUrl = ref("");
const dateRange = ref([]);
const dateRange1 = ref([]);
const pageList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const approveStatusList = ref([
  {
    label: "停用",
    value: "1",
    elTagClass: "",
    elTagType: "danger",
  },
  {
    label: "启用",
    value: "0",
    elTagClass: "",
    elTagType: "success",
  },
]);

const approveList = ref([
  { label: "停用", value: 1 },
  { label: "启用", value: 0 },
]);
const openSub = ref(false); // 查看购买详情
const loadingSub = ref(false);
const pageListSub = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
  rules: {
    dictName: [{ required: true, message: "字典不能为空", trigger: "blur" }],
    dictType: [
      { required: true, message: "字典编码不能为空", trigger: "blur" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询公告列表 */
function getList() {
  loading.value = true;
  let sendData = queryParams.value;

  listSysDicts(sendData).then((response) => {
    pageList.value = response.data.rows;
    total.value = response.data.count;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {};

  proxy.resetForm("noticeRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  queryParams.value.queryUserName = undefined;
  queryParams.value.queryContent = undefined;
  queryParams.value.queryApprove = undefined;

  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "新增";
}

/**修改按钮操作 */
function handleUpdate(row) {
  reset();
  {
    form.value = {
      ...row,
    };
  }

  open.value = true;
  title.value = "修改";
}
// 防抖操作
const debouncedFn = useDebounceFn(
  () => {
    submitForm();
  },
  1000,
  { maxWait: 3000 }
);

/** 提交按钮 */
function submitForm() {
  proxy.$refs["noticeRef"].validate((valid) => {
    if (valid) {
      let sendData = {
        dictName: form.value.dictName,
        dictType: form.value.dictType,
        remarks: form.value.remarks || "",
      };
      if (form.value.id) {
        sendData.id = form.value.id;
        sendData.status = form.value.status;
        updateSysDicts(sendData).then((response) => {
          proxy.$modal.msgSuccess("操作成功！");
          open.value = false;
          getList();
        });
      } else {
        addSysDicts(sendData).then((response) => {
          proxy.$modal.msgSuccess("操作成功！");
          open.value = false;
          getList();
        });
      }
    }
  });
}

// 排序
function handleSortChange(column) {
  let data1;
  if (column.order == "ascending") {
    data1 = "ASC";
  } else if (column.order == "descending") {
    data1 = "DESC";
  }
  if (!data1) {
    data.queryParams.orderName = undefined;
  } else {
    data.queryParams.orderName = column.prop;
  }
  data.queryParams.orderType = data1;
  getList();
}
getList();
</script>
<style lang="scss" scoped>
.blue-color {
  color: blue;
}
</style>
